package net.example.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.example.model.ImageTextModel;
import net.example.ggtalk.R;

import java.util.List;

public class ListViewImageTextAdapter extends BaseAdapter {

    private List<ImageTextModel> modelList;
    private Context context;


    public ListViewImageTextAdapter(List<ImageTextModel> modelList,Context context)
    {
        this.modelList=modelList;
        this.context=context;
    }
    @Override
    public int getCount() {
        return modelList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.modelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        try{
            ListViewImageTextAdapter.ViewHolder holder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(
                        R.layout.listview_item_imagetext, parent, false);
                holder = new ListViewImageTextAdapter.ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ListViewImageTextAdapter.ViewHolder) view.getTag();
            }
            ImageTextModel model = this.modelList.get(position);
            int imageResource=model.getImageResource();
            if(imageResource==0)
            {
                holder.headerImg.setVisibility(View.GONE);
            }
            else
            {
                holder.headerImg.setImageResource(imageResource);
            }
            holder.userName.setText(model.getText());
            view.setOnClickListener(model.getHandler());
        }catch (Exception ex){ex.printStackTrace();}
        return view;
    }

    private class ViewHolder
    {
        public ImageView headerImg;
        public TextView userName;

        public ViewHolder(View view)
        {
            this.headerImg= (ImageView) view.findViewById(R.id.listview_iv_ico);
            this.userName=(TextView) view.findViewById(R.id.listview_tv_itemName);
        }
    }
}
